﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

using PowerCreator.LMS.Web.Project;
using PowerCreator.LMS.Common.Utils;
using PowerCreator.LMS.Common.Script;

using PowerCreator.LMS.Web.DL.Project;
using PowerCreator.LMS.DL.Project.Entity;

public partial class Theme_3_StudyCenter : System.Web.UI.Page
{
    PowerCreator.LMS.ViewQuery.ViewQuery opViewQuery = new PowerCreator.LMS.ViewQuery.ViewQuery();
    PowerCreator.LMS.Web.Course.Resource opResource = new PowerCreator.LMS.Web.Course.Resource();
    PowerCreator.LMS.Web.Channel.Channel opChannel = new PowerCreator.LMS.Web.Channel.Channel();
    protected PowerCreator.LMS.Web.Config.WebConfig config = new PowerCreator.LMS.Web.Config.WebConfig();
    private int theDayNumOfWeek = 1;
    protected string HtmlLastUploadResource = "";
    protected string HtmlPublicCourse = "";
    //protected string HtmlMyStowResource = "";
    protected string HtmlMyProjectList = "";
    ProjectStudent opProjectStudent = new ProjectStudent();
    ProjectUser opProjectUser = new ProjectUser();
    NewProjectPlan opProjectPlan = new NewProjectPlan();
    Project opProject = new Project();
    PowerCreator.LMS.Web.CMS.New opNews = new PowerCreator.LMS.Web.CMS.New();
    //ProjectStudent opProjectStudent = new ProjectStudent();
    PowerCreator.LMS.Web.Download.Download opDownload = new PowerCreator.LMS.Web.Download.Download();
    PowerCreator.LMS.ViewQuery.ViewQuery ov = new PowerCreator.LMS.ViewQuery.ViewQuery();
    PowerCreator.LMS.Web.Course.Resource op = new PowerCreator.LMS.Web.Course.Resource();
    PowerCreator.LMS.Web.Course.Course opCourse = new PowerCreator.LMS.Web.Course.Course();
    private PowerCreator.LMS.Web.DL.User.LoginEntity login = new PowerCreator.LMS.Web.DL.User.LoginEntity();
    private string url = "UserLogin.aspx";

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Expires = -1;
        CheckUser();
        this.Title = config.GetLanagerValueByKey("WebTitle", PowerCreator.LMS.Language.LanagetItemType.WebTitle); //"PowerCreator 学习管理系统";
        if (!IsPostBack)
        {
            //BinderNews();
            //BinderCourse();
            //BinderMyStowResoure();
            //HtmlMyStowResource=BinderMyStowResoure();
            BinderMyProjects();
            BinderRPT();
        }
    }

    private void CheckUser()
    {
        if (Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO] == null)
        {
            Script.RedirectURL(url);
        }

        login = (PowerCreator.LMS.Web.DL.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];
        if (login == null)
        {
            Script.RedirectURL(url);
        }

    }
    //protected override void InitPage()
    //{
    //    base.InitPage();
    //}

    //private void BinderCourse()
    //{
    //    VOTOSchoolStage vostage = new VOTOSchoolStage();
    //    TOSchoolStage objstage = new TOSchoolStage();
    //    PowerCreator.LMS.Web.Catalog.Catalog objcatalog=new PowerCreator.LMS.Web.Catalog.Catalog();
    //    PowerCreator.LMS.Course.Entity.VOTCatalog vocatalog = new PowerCreator.LMS.Course.Entity.VOTCatalog();
    //    PowerCreator.LMS.Web.Course.Course opCourse = new PowerCreator.LMS.Web.Course.Course();
    //    string search = " CourseState = 1 and";
    //    search += "  Course_Id in (select FKUCCourseID from UserCourse where UCState=1 and UCUserID=" + login.UserID + " )";
    //    string orderList = "CourseCreatetime desc,CourseId desc";
    //    DataTable dt = opCourse.QueryCourse(search, orderList, 4, 1);

    //    dt.Columns.Add("strxueke");
    //    dt.Columns.Add("strxueduan");
    //    for (int i = 0; i < dt.Rows.Count; i++)
    //    {
    //        vocatalog=objcatalog.Load(int.Parse(dt.Rows[i]["xueke"].ToString()));
    //        dt.Rows[i]["strxueke"] = vocatalog.CatalogName;
    //        vostage=objstage.Load(int.Parse(dt.Rows[i]["xueduan"].ToString()));
    //        dt.Rows[i]["strxueduan"] = vostage.SchoolStage;
    //    }
    //    rpt_MyCourse.DataSource = dt;
    //    rpt_MyCourse.DataBind();

    //}
    private void BinderMyProjects()
    {
        VOTOAdvancedUser voteacher = new VOTOAdvancedUser();
        TOAdvancedUser objteacher = new TOAdvancedUser();
        PowerCreator.LMS.ViewQuery.ViewQuery obj = new PowerCreator.LMS.ViewQuery.ViewQuery();
        string search = " SELECT TO_Class.Name, TO_Class.Plans_Id, TO_Class.Batch_Id, TO_Class.[Rule], TO_UserOfClass.UserID, TO_UserOfClass.ClassID,TO_Class.ClassCourseListID, TO_CourseList.CourseList_Id, TO_CourseList.CourseListName,TO_Class.ClassLecturer, TO_Class.ClassLeader, TO_Class.ClassAssistant FROM TO_UserOfClass ,TO_Class,TO_CourseList Where TO_UserOfClass.ClassID = TO_Class.Class_Id AND TO_Class.ClassCourseListID = TO_CourseList.CourseList_Id and TO_CourseList.Used=1 and TO_UserOfClass.UserID=" + login.UserID;
        DataTable dt = new DataTable();
        dt = obj.Query(search);
        dt.Columns.Add("zhujian");
        dt.Columns.Add("banzhuren");
        dt.Columns.Add("fudaoyuan");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            voteacher = objteacher.Load(int.Parse(dt.Rows[i]["ClassLecturer"].ToString()));
            dt.Rows[i]["zhujian"] = voteacher.Name;
            voteacher = objteacher.Load(int.Parse(dt.Rows[i]["ClassLeader"].ToString()));
            dt.Rows[i]["banzhuren"] = voteacher.Name;
            voteacher = objteacher.Load(int.Parse(dt.Rows[i]["ClassAssistant"].ToString()));
            dt.Rows[i]["fudaoyuan"] = voteacher.Name;
        }
        rpt_TrainProject.DataSource = dt;
        rpt_TrainProject.DataBind();
    }

    protected void rpt_TrainProject_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName.ToString() == "lstudy")
        {
              Label lbl_CourseListID=(Label)e.Item.FindControl("lbl_CourseListID");  //课程表ID
              string url = "StudyProject.aspx?ClassID=" + e.CommandArgument.ToString() + "&CourseListID=" + lbl_CourseListID.Text;
            PowerCreator.LMS.Common.Script.Script.RedirectURL(url);
        }
    }
    //protected void rpt_MyCourse_ItemCommand(object source, RepeaterCommandEventArgs e)
    //{
    //    if (e.CommandName.ToString() == "lshowCourse")
    //    {
    //        string url = "OpenCourse.aspx?CourseID=" + e.CommandArgument.ToString() + "&ClassID=0";
    //        PowerCreator.LMS.Common.Script.Script.RedirectURL(url);
    //    }
    //}
    

    private string GetNewSearch()
    {
        string sNewIDList = "0";
        string search = "";
        string rtsearch = "";

        DataTable dt = new DataTable();
        DataTable dt2 = new DataTable();
        string sql = "";
        search = "SELECT * FROM TO_UserOfClass where UserID=" + login.UserID;
        dt = ov.Query(search);
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            sql = "select NewID from TNew where  FKNewTrainclaIDList like '%," + dt.Rows[i]["ClassID"].ToString() + ",%'";
            dt2 = ov.Query(sql);
            for (int k = 0; k < dt2.Rows.Count; k++)
            {
                sNewIDList += "," + dt2.Rows[k]["NewID"].ToString();
            }
            dt2.Clear();
        }
        rtsearch = " NewState=1 and NewID in (" + sNewIDList + ")";
        return rtsearch;
    }

    private void BinderRPT()
    {
        DataTable dt = new DataTable();
        string orderby = "NewID desc";
        string search = GetNewSearch();
        int iPageSize = 3;
        dt = opNews.Query(search, orderby, iPageSize, 1);
        if (dt.Rows.Count == 0)
        {
            td_MyNew.Visible = false;
            br_MyNew.Visible = false;
            return;
        }
        rpt_New.DataSource = dt;
        rpt_New.DataBind();
    }


    //private void BinderMyStowResoure()
    //{
    //    string search = " ResourceID in";
    //    search += "  (select FKResourceID from TUserStowResource where FKUserID=" + login.UserID + " )";
    //    string orderList = " ResourceID desc";
    //    DataTable dt = op.QueryResource(search, orderList, 4, 1);
    //    dt.Columns.Add("ResourceImageUrl");
    //    dt.Columns.Add("OpenUrl");
    //    int t = 0;
    //    if (!PowerCreator.LMS.SCR.Dog.CheckDog())
    //    {

    //        for (int i = dt.Rows.Count - 1; i > 1; i--)
    //        {
    //            dt.Rows[i].Delete();
    //            t = t + 1;
    //        }

    //    }
    //    if (dt.Rows.Count == 0)
    //    {
    //        dt_MyStowResource.Visible = false;
    //        return;
    //    }
    //    for (int i = 0; i < dt.Rows.Count - t; i++)
    //    {
    //        dt.Rows[i]["OpenUrl"] ="OpenCourseware.aspx?ResourceID=" +dt.Rows[i]["ResourceID"].ToString()  + "&CourseResourceID=0&CourseID=0";
    //        if (dt.Rows[i]["ResourceClass"].ToString() == "1" || dt.Rows[i]["ResourceClass"].ToString() == "2")  //课件资源
    //        {
    //            string CoursersResourceUrl = GetPath(dt.Rows[i]["ResourceUrl"].ToString(), int.Parse(dt.Rows[i]["ResourceClass"].ToString()));
    //            dt.Rows[i]["ResourceImageUrl"] = CoursersResourceUrl + GetPicName(dt.Rows[i]["ResourceEntry"].ToString(), int.Parse(dt.Rows[i]["ResourceClass"].ToString()));
    //        }
    //        else
    //        {
    //            dt.Rows[i]["ResourceImageUrl"] = "images/Exam.jpg";
    //        }
    //    }

    //    rpt_MyStowResource.DataSource = dt;
    //    rpt_MyStowResource.DataBind();
    //}


    private string GetPicName(string fileName, int Resourceclass)
    {
        string rt = "images/gkkpic.jpg";
        if (Resourceclass == 1)
        {
            rt = "000.jpg";
        }
        else if (Resourceclass == 2)
        {
            string[] fileNames = fileName.Split('.');
            if (fileNames.Length == 2)
                rt = fileNames[0].ToString() + ".jpg";
        }
        return rt;
    }
    private string GetPath(string Path, int Resourceclass)
    {
        string rt = Path;
        if (Resourceclass != 1 && Resourceclass != 2)
        {
            rt = "";
        }
        return rt;
    }



}
